Solved

Excel - VBA - Problem with code - add data

Posted on 2004-08-20
6
290 Views
Last Modified: 2010-05-02
Hi - i'm having problems with the following code. The code works, but it bebug.print "count" like 30 times in the Immediate window and I'm not sure why it's looping so much and how to fix.  Can anyone help?

Thanks

'---------------------------------------------------------------------------------
Private Sub FileName()
    Const strSlash As String = "\"
    Dim strPath As String
    Dim strFile As String    
    Dim ws As Worksheet
   
    Set ws = Application.Workbooks(1).Worksheets("Attribute_Sheet")
   
    Call GetCell2
   
    'returns the value of the desired cells
    strPath = ws.Cells(strRowCount - 1, strColumnCount)
   
    'splits the values out
    strFile = Split(strPath, strSlash)(UBound(Split(strPath, strSlash)))

    Debug.Print strFile
   
    'writes the filename to desired cell
    Worksheets("Attribute_Sheet").Range("D3").Value = strFile
   
     'not using, but generates the same error
     'ws.Cells(strRowCount1 - 1, "A").Value = strFile

    Debug.Print "count"
End Sub
'---------------------------------------------------------------------------------
Private Sub GetCell2()
       
    'gets current row number
    strRowCount = ActiveCell.Row
   
    'gets current column number
    strColumnCount = ActiveCell.Column
   
End Sub
'---------------------------------------------------------------------------------
0
Comment
Question by:eciabattari
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:Colosseo
ID: 11857774
Hi eciabattari

could you please give an explanation of how you are using the code.

Do you call FileName from another sub?

are strRowCount  and strColumnCount declared globally?

Cheers

Scott
0
 
LVL 8

Expert Comment

by:ampapa
ID: 11858244
There is no loop in the above code so I'm not sure why you think it's looping. If your calling this multiple times and not resetting your count that could be why you count reflects "30". Other than that as Scott said we need to see a bit more of your code or how you are using the above segment.

ampapa,
0
 

Author Comment

by:eciabattari
ID: 11859368
I'm using the following code within Excel VBA.

No, I do not call FileName from another sub.  FileName is called from Worksheet_Change, code below.

Yes, strRowCount and strColumnCount are declared globally.

I'm not calling it multiple times but when a change is made to the worksheet.  The code works when I physicaly type or modify data within a cell; however, if I try to paste 10 rows of data within the column, it doesn't work.  Any ideas on how to make the code work when pasting data wtihin the desired column.

Thanks for the help.

'---------------------------------------------------------------------------------
Private Sub Worksheet_Change(ByVal Target As Range)

    'declarations
    Dim rg As Range
    Dim strFinalEntry As String
    Dim strInputCell As String
   
    'gets current cell that is used to determin what column the user is in.
    Call GetCell
   
    'ensure only the proper columns are affected
    'column "5" - E is used for Category and Sub-Category values
    'column "3" - C is used for filename and DCTM Path commands
   
    If strColumn = "5" Then
       
        'sets cell working on and target cell
        strFinalEntry = strColumnValue & strRow
        strInputCell = strColumnValue1 & strRow
       
        Set rg = Application.Intersect(Target, Range(strInputCell))
       
        'clears the target cell if a value is present
        If Not rg Is Nothing Then
            Application.EnableEvents = False
            Range(strFinalEntry).ClearContents
            Application.EnableEvents = True
        End If
       
        'creates new InCell dropdown list within target cell
        Call FormatWorksheet
       
    ElseIf strColumn = "3" Then
       
        'sets cell working on and target cell
        strInputCell = strColumnValue1 & strRow - 1
        strFinalEntry = "A" & strRow - 1
       
        'returns filename and places it within A column
        Call FileName
        'Call UseReplace
       
    Else
        'do nothing
    End If
 
End Sub
'---------------------------------------------------------------------------------
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:ampapa
ID: 11859537
Filename is being calle by a "worksheet" change so every time you type or change the worksheet it is being called. What does "Call FormatWorksheet" look like?
0
 
LVL 8

Accepted Solution

by:
ampapa earned 500 total points
ID: 11859751
You have a slight typo iin using you column and row counts. The variables are strRowCount and strColumnCount not strRow and strColumn. That could be part of your problem. I would also make them global as Colosseo suggested.

    If strColumn = "5" Then                <======================
       
        'sets cell working on and target cell
        strFinalEntry = strColumnValue & strRow           <========================
        strInputCell = strColumnValue1 & strRow           <========================
       
        Set rg = Application.Intersect(Target, Range(strInputCell))
       
        'clears the target cell if a value is present
        If Not rg Is Nothing Then
            Application.EnableEvents = False
            Range(strFinalEntry).ClearContents
            Application.EnableEvents = True
        End If
       
        'creates new InCell dropdown list within target cell
        Call FormatWorksheet
       
    ElseIf strColumn = "3" Then                   <=======================
0
 

Author Comment

by:eciabattari
ID: 11862338
I found out the problem and removed the code below from FileName and placed it into
Private Sub Worksheet_Change(ByVal Target As Range).  It corrected the problem.  I'm still not sure why but it did.  Thanks for the help.


'-----------------------------------
Dim ws As Worksheet  
Set ws = Application.Workbooks(1).Worksheets("Attribute_Sheet")
ws.Cells(strRowCount1 - 1, "A").Value = strFile
'-----------------------------------
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question