[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Excel - VBA - Problem with code - add data

Posted on 2004-08-20
6
Medium Priority
?
299 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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

649 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