Solved

Excel - VBA - Problem with code - add data

Posted on 2004-08-20
6
256 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
  • 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now