0

I am having trouble debugging a code I wrote. It runns fine until a certain point and then stops and shows: runtime error 1004 application-defined or object-defined error.

I have not written VBA in a very long time so my code might be a mess =).

The problem seems to be with: see comment 'Generate UID for the Current Imp

'Sheet Definitions strSourceSheet = "MasterReport" strComponentSheet = "UniqueIdComponents" 'defines row where data starts intEntryCount = 2 intImpCount = 0 'determine maximum number of rows for both sheets lngMaxRowSS = ThisWorkbook.Sheets(strSourceSheet).UsedRange.SpecialCells(xlCellTypeLastCell).Row lngMaxRowCS = ThisWorkbook.Sheets(strComponentSheet).UsedRange.SpecialCells(xlCellTypeLastCell).Row 'Run until there are no more entries For intEntryCount = 2 To lngMaxRowSS 'Prevents to overwrite existing UIDs If ThisWorkbook.Sheets(strSourceSheet).Range("BP" & intEntryCount) = "" Then 'Recieve next imperative on the Source List to find according UID Components strSourceImperative = ThisWorkbook.Sheets(strSourceSheet).Range("A" & intEntryCount) 'Run until no new Imp UID is defind For intImpCount = 11 To lngMaxRowCS 'Location of Imps on Component Sheet strComponentImperative = ThisWorkbook.Sheets(strComponentSheet).Range("C" & intImpCount) ' If the Source Imp = Component Imp then we create a UID for that Source IP If strSourceImperative = strComponentImperative Then 'Assign Column to UID component in order to find the Column in the MasterReport strUIDComponent1 = ThisWorkbook.Sheets(strComponentSheet).Range("D" & intImpCount) strUIDComponent2 = ThisWorkbook.Sheets(strComponentSheet).Range("E" & intImpCount) strUIDComponent3 = ThisWorkbook.Sheets(strComponentSheet).Range("F" & intImpCount) strUIDComponent4 = ThisWorkbook.Sheets(strComponentSheet).Range("G" & intImpCount) strUIDComponent5 = ThisWorkbook.Sheets(strComponentSheet).Range("H" & intImpCount) strUIDComponent6 = ThisWorkbook.Sheets(strComponentSheet).Range("I" & intImpCount) strUIDComponent7 = ThisWorkbook.Sheets(strComponentSheet).Range("J" & intImpCount) 'Generate UID for the Current Imp strUID = ThisWorkbook.Sheets(strSourceSheet).Range(strUIDComponent1 & intEntryCount) _ & ThisWorkbook.Sheets(strSourceSheet).Range(strUIDComponent2 & intEntryCount) _ & ThisWorkbook.Sheets(strSourceSheet).Range(strUIDComponent3 & intEntryCount) _ & ThisWorkbook.Sheets(strSourceSheet).Range(strUIDComponent4 & intEntryCount) _ & ThisWorkbook.Sheets(strSourceSheet).Range(strUIDComponent5 & intEntryCount) _ & ThisWorkbook.Sheets(strSourceSheet).Range(strUIDComponent6 & intEntryCount) _ & ThisWorkbook.Sheets(strSourceSheet).Range(strUIDComponent7 & intEntryCount) 'Writes UID into MasterReport 'ThisWorkbook.Sheets(strSourceSheet).Range("BP" & intEntryCount) = strUID 'Test Writes ThisWorkbook.Sheets("Test").Range("A" & intEntryCount) = strUID 'If the Source Imp = Component Imp then we created a UID for that Source IP End If 'If the two Source Imp <> Component Imp, go to next row on Component sheet and compare again Next intImpCount 'Prevented to overwrite existing UIDs End If Next intEntryCount 

In the case where i get the error the components are A,M,N,O,BK,"","" and the Entry count is 5718. It wrote 5718 entries just fine and then shows the Error.

Any Ideas?

Thanks in advance for your help!!

2
  • 1
    The code you have posted does not make any sense at all. Perhaps you should post more of your code and describe what you are trying to achieve with it. Commented Jan 13, 2014 at 14:22
  • Here is what it does: strUID is a Unique ID that is composed of a number of Cell Values. These values can be different depending on how many and which components (columns) a user chooses. since the values of the components are stored in cells, I use the strUIDComponent(s) to tell me which column and intEntryCount which row. I create the strUID by adding multiple cells together (see code) and then print it in a different sheet. Commented Jan 13, 2014 at 14:33

1 Answer 1

2

Taking ThisWorkbook.Sheets(strSourceSheet) out to a With statement and substituting values, you appear to be saying this statement is equivalent to:

With ThisWorkbook.Sheets(strSourceSheet) strUID = .Range("A" & 5718) & _ .Range("M" & 5718) _ .Range("N" & 5718) _ .Range("O" & 5718) _ .Range("BK" & 5718) _ .Range("" & 5718) _ .Range("" & 5718) End With 

The last two entries are not valid ranges.

I cannot test this code but something like this might meet your requirements:

 Dim WkShtComp As Worksheet Dim WkShtSrc As Worksheet Dim ColMast As String With ThisWorkbook Set WkShtComp = .Sheets(strComponentSheet) Set WkShtSrc = .Sheets(strSourceSheet) End With strUID = "" For Each ColMast in Array("D", "E", "F", "G", "H", "I", "J") strUIDComponent = WKShtComp.Range(ColMast & intImpCount).Value If strUIDComponent <> "" Then strUID = strUID & WkShtSrc.Range(strUIDComponent & intEntryCount).Value Endif Next 
Sign up to request clarification or add additional context in comments.

2 Comments

Your code is what it would look like, yes. I also thought that the "" might be a problem, but until 5718 it worked and I had many such UID's missing the last 2 components (that would have been ""). and it wrote them just fine. But since it is not proper code, i would rather fix it. Any ideas how i could exclude the ""'s without having to write 7 seperate IF statements? I am not really good with VBA so that is about as creative as i can get haha
It is difficult to make suggestions without a better understanding of what you are doing. I would consider making strUIDComponent an array or going make a step and making strUID in a loop directly without use the strUIDComponents as intermediaries.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.