Using VBA to copy data into another worksheet ???


R

robinscoe

I have an Excel workbook with 5 worksheets (individuals fill out their appropriate worksheet columns A to AW). There is a Totals worksheet in the workbook that has a command click update button. The update button starts with the 1st individual worksheet and using column A (until column A is empty)copies the information onto the Totals worksheet....it then moves to the next individual worksheet and does the same thing. The problem is that it appears vba will only copy data from column A to Z, what do I need to do in order to pick up the remaining 23 columns that contain data?
 
Ad

Advertisements

G

Gord Dibben

VBA recognizes all columns in a worksheet. . . . at least in 2007 it
does.

Sub copycols()
Sheets("Sheet1").Range("A1:AZ12").Copy _
Destination:=Sheets("Sheet2").Range("A1")
End Sub


Gord
 
Ad

Advertisements

R

robinscoe

I have an Excel workbook with 5 worksheets (individuals fill out their appropriate worksheet columns A to AW). There is a Totals worksheet in the workbook that has a command click update button. The update button starts with the 1st individual worksheet and using column A (until column A is empty)copies the information onto the Totals worksheet....it then moves to the next individual worksheet and does the same thing. The problem is that it appears vba will only copy data from column A to Z, what do I need to do in order to pick up the remaining 23 columns that contain data?
That seems to work if I specify the ending row, which is unknown. My code is:
Private Sub cmdUpdate_Click()
Dim w As String
Dim x As String
Dim y As String
Dim z As String
Dim lCount As Long
Dim lresult As Long
Dim lcellrangenumeric As Long
Dim lColIndex As Long
Dim lSheetIndex As Integer

With Worksheets("Total")

lCount = 3 ' start row value
While Len(.Range("A" & CStr(lCount))) > 0
For lColIndex = Asc("A") To Asc("AW") ' column value
.Range(Chr(lColIndex) & CStr(lCount)).Value = ""
Next lColIndex
lCount = lCount + 1 'next row
Wend
End With

For Each ws In Worksheets
With ws
If InStr(ws.Name, ".") > 0 Then ' does this worksheet contain someone's name?
' transfer data
lCount = 2 ' start row value
While Len(.Range("A" & CStr(lCount))) > 0
lresult = FindnextAvail ' first available row in the Totals sheet
For lColIndex = Asc("A") To Asc("AW") ' column value
Worksheets("Total").Range(Chr(lColIndex) & CStr(lresult)).Value = .Range(Chr(lColIndex) & CStr(lCount)).Value
Next lColIndex
lCount = lCount + 1 'next row
Wend

End If
End With
Next ws

End Sub

Private Function FindnextAvail()
Dim lCount As Long

With Worksheets("Total")
lCount = 3 ' start row value
While Len(.Range("A" & CStr(lCount))) > 0
lCount = lCount + 1 'next row
Wend
End With

FindnextAvail = lCount
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top