VBA having column name and value down the left of the worksheet

nutbolt

New Member
Joined
Apr 2, 2021
Messages
17
Office Version
  1. 2019
Rather than have all my column names across the top of the worksheet, I'm looking to have them down the left hand side eg in Column A. The value of that will then be in Column B. I have used a recordset which loops through and pulls the first column correctly, but I need 30 more columns to do the same with the values. Where am I going wrong?

VBA Code:
' Run the query and store the retrieved data in the recordset
Set adoRecordset = adoCommand.Execute
' Set to the first empty row
intRow = 10
' Loop through the recordset
Do While Not adoRecordset.EOF
    For rs = 0 To adoRecordset.Fields.Count - 1
    
        With shtData
                     
                With .Cells(intRow, intCol + 1)
               
                ' Write the data to the cells
                .Cells(intRow, intCol + 1).Value = adoRecordset.Fields(intCol).Name
                .Cells(intRow, intCol + 2).Value = adoRecordset.Fields(intCol).Value
          
                End With
           
        End With
    ' Go to the next column in the record
    Next rs
    'Next intRow
    
    ' Go to the next row
    intRow = intRow + 1
    ' Move to the next record in the recordset
    adoRecordset.MoveNext
Loop
' Reset the recordset
Set adoRecordset = Nothing
' Reset the command
Set adoCommand = Nothing
' Closet and reset the connection
adoConnection.Close
Set adoConnection = Nothing
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I suppose you are looking for transposing.

VBA Code:
Sub Transpose_Example2()
  Dim lRow As Long

  ActiveSheet.UsedRange.Cut 'Or your data range like Range("A1:C5")
  Range("A1").PasteSpecial Transpose:=True
 'Delete after this part if you don't want reverse order
  lRow = Cells(Rows.Count, 1).End(xlUp).Row
  For i = 2 To lRow
        Cells(i, 1).EntireRow.Cut
        Cells(1, 1).EntireRow.Insert Shift:=xlDown
  Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top