s0up2up
s0up2up

Reputation: 59

For each cell in worksheets("xyz") query

Idea behind the code is that it is supposed to find customer names, then find the level of sales to them throughout the year then paste all this data into another sheet.

Getting a Run-Time error '1004' Application-defined or object-defined error from the following line. I have asterisked the line where I am getting the error.

Sub Import_CustomerData()

Dim strMonth As String
Dim rngMonth As Range

Dim DataImportColum As Integer
Dim DataImportRow As Integer

Dim strFirstCustomer As String
Dim strSecondCustomer As String
Dim strThirdCustomer As String
Dim strFourthCustomer As String
Dim strFifthCustomer As String

Dim lngFirstCustomerSales As Long
Dim lngSecondCustomerSales As Long
Dim lngThirdCustomerSales As Long
Dim lngFourthCustomerSales As Long
Dim lngFifthCustomerSales As Long
Dim lngTotalSales As Long

Dim cell As Range
Dim x As Integer

'Finding Data for clients
For Each cell In Worksheets("Data entry").Range("A1:A99")

If cell.Value = "Customer Sales" Then

    strFirstCustomer = cell.Offset(1, 0).Value
    strSecondCustomer = cell.Offset(2, 0).Value
    strThirdCustomer = cell.Offset(3, 0).Value
    strFourthCustomer = cell.Offset(4, 0).Value
    strFifthCustomer = cell.Offset(5, 0).Value

End If

Next

'Extracting Data from Customer sheet

***For Each cell In Worksheets("Client_Customer").Range("B83:86")***

'First Customer
If cell.Value = strFirstCustomer Then
    lngFirstCustomerSales = Val(cell.Offset(0, 1))
End If

'Second Customer
If cell.Value = strSecondCustomer Then
    lngSecondCustomerSales = Val(cell.Offset(0, 1))
End If

'Third Customer
If cell.Value = strThirdCustomer Then
    lngThirdCustomerSales = Val(cell.Offset(0, 1))
End If

'Fourth Customer
If cell.Value = strFourthCustomer Then
    lngFourthCustomerSales = Val(cell.Offset(0, 1))
End If

'Fifth Customer
If cell.Value = gxdfg Then
    lngFifthCustomerSales = Val(cell.Offset(0, 1))
End If

'Total Customers Sales
If cell.Value = "Total:" Then
    lngTotalSales = Val(cell.Offset(0, 1))
End If

Next

'Importing it into Data Customer Monthly 2013 sheet.

'Determing month of client system reports
    strMonth = Sheets("Client_Customer").Range("B8").Value

If strMonth = "" Then

    frmEnter_month.Show

Else

    iLenMonth = Len(strMonth)
       x = iLenMonth - 5
            strLeftMonth = Left(strMonth, x)

End If

'To find Column of Customer imput
For Each cell In Range("B4:M4")

        If cell.Value = strLeftMonth Then
            DataImportColumn = cell.Column

        End If

Next

For Each cell In Worksheets("data customer monthly 2013").Range("A3:A9999")

'First Customer
If cell.Value = strFirstCustomer Then
        DataImportRow = cell.Row

** 2 ** lngFirstCustomerSales = Cells(DataImportRow, DataImportColumn).Offset(0, 2).Value ** 2 **
End If

'Second Customer
If cell.Value = strSecondCustomer Then
        DataImportRow = cell.Row

        lngSecondCustomerSales = Cells(DataImportRow, DataImportColumn).Offset(0, 2).Value
 End If

'Third Customer
If cell.Value = strThirdCustomer Then
        DataImportRow = cell.Row

         lngThirdCustomerSales = Cells(DataImportRow, DataImportColumn).Offset(0, 2).Value
 End If

'Fourth customer
If cell.Value = strFourthCustomer Then
        DataImportRow = cell.Row

    lngFourthCustomerSales = Cells(DataImportRow, DataImportColumn).Offset(0, 2).Value 
 End If

'Fifth Customer
If cell.Value = strFifthCustomer Then
        DataImportRow = cell.Row

    lngFifthCustomerSales = Cells(DataImportRow, DataImportColumn).Offset(0, 2).Value
 End If

'Total Sales
If cell.Value = "Total Sales" Then
        DataImportRow = cell.Row

    lngTotalSales = Cells(48, DataImportColumn).Value 
End If

Next

DeleteClientSheets

End Sub

Sorry for the large amount of code but does anyone have any suggestions? Couldn't find anything else that help explain the question as cell has been defined as a range.

EDIT1:

Second question: After Silenxor's brilliant solution, I am getting code on the line with the following indicator: ** 2 **

The error I am getting is the same as the first error.

Upvotes: 0

Views: 10270

Answers (1)

Silenxor
Silenxor

Reputation: 246

With regards to your asterix line

For Each cell In Worksheets("Client_Customer").Range("B83:86")

Try

For Each cell In Worksheets("Client_Customer").Range("B83:B86")

Upvotes: 1

Related Questions