?
Solved

Excel - VBA - Problem with code - add data

Posted on 2004-08-20
6
Medium Priority
?
297 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
Technology Partners: 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 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

752 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