Excel - VBA - Problem with code - add data

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
'---------------------------------------------------------------------------------
eciabattariAsked:
Who is Participating?
 
ampapaConnect With a Mentor Commented:
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
 
ColosseoCommented:
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
 
ampapaCommented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
eciabattariAuthor Commented:
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
 
ampapaCommented:
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
 
eciabattariAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.