Solved Access VBA To Concatenate Excel Cells (2 Viewers)

Isaac

Lifelong Learner
Local time
Today, 13:35
Joined
Mar 14, 2017
Messages
8,869
Also, don't do this:
Code:
Dim lColumn, x As Long
That doesn't do what you are thinking it does. It types IColumn as a Variant.
 

Isaac

Lifelong Learner
Local time
Today, 13:35
Joined
Mar 14, 2017
Messages
8,869
And you haven't properly closed the Excel app..........you are going to have a bunch of Excel.Exe's in your task manager with that code. Left hanging.
 

jo15765

Registered User.
Local time
Today, 13:35
Joined
Jun 24, 2011
Messages
130
well the problem is maybe this line, which isn't what I provided:

wb.Sheets(1).Cells.Value = strValue

what are you actually trying to do - set the value of every single cell in the workbook (billions of cells) to that value?

I want to concatenate the values of B- F in column G with a period between them. But I don't want to hardcode B:F because those columns can be dynamic

What is the proper way to close Excel?
 

jo15765

Registered User.
Local time
Today, 13:35
Joined
Jun 24, 2011
Messages
130
All rows with data. (sans the first row since it will always be the header row)
 

Isaac

Lifelong Learner
Local time
Today, 13:35
Joined
Mar 14, 2017
Messages
8,869
Oh, you hadn't mentioned this! Then you are going to need more code (an Outer loop) within which to nest your inner loop.
Which column always has data - is A reliable?
 

jo15765

Registered User.
Local time
Today, 13:35
Joined
Jun 24, 2011
Messages
130
Oh, you hadn't mentioned this! Then you are going to need more code (an Outer loop) within which to nest your inner loop.
Which column always has data - is A reliable?

Yes column A will always hold the name. Then columns B - ? will hold the counts that I need to concatenate (it varies form workbook to workbook)
 

Isaac

Lifelong Learner
Local time
Today, 13:35
Joined
Mar 14, 2017
Messages
8,869
Ok, you took my code and changed stuff and dropped stuff to where lColumn was missing its assignment value.
Here is my new code. If you take this and drop stuff and change stuff and then post it back, this back and forth will not work.
This is aircode, if you see an obvious typo you may need to change it.

Code:
Function Test()
Dim wb As Excel.Workbook
Dim xlApp As Excel.Application
Dim lColumn as Long, x As Long
Dim strValue As String
Dim rng As Object, ws as object, y as long

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False

Set wb = xlApp.Workbooks.Open("C:\Test\Test.xlsx", False, False)
set ws = wb.sheets(1)
'wb.Sheets(1).Select '''''don't ever use Select or Activate in Excel VBA
dim lastrow as long, lColumn as long
lastrow = ws.range("A" & ws.rows.count).end(-4162).row
lColumn = ws.range("A" & ws.columns.count).end(-4159).column
for y = 2 to lastrow 'start on row2
    strValue=""
   for x = 2-lColumn 'start at col B
           strValue = strValue & "." & ws.Cells(y, x)
   next x
    strValue = Right(strValue, Len(strValue) - 1)
    ws.cells(y,lColumn+1).value=strValue
next y

wb.Save
wb.Close
xlApp.displayalerts=false
xlApp.Quit

End Function
 

jo15765

Registered User.
Local time
Today, 13:35
Joined
Jun 24, 2011
Messages
130
I'm getting an error on the line
Code:
For x = 2 -lColumn 'start at col B

it says Expected: To
 

Isaac

Lifelong Learner
Local time
Today, 13:35
Joined
Mar 14, 2017
Messages
8,869
Sorry, make it
Code:
For x = 2 to lColumn 'start at col B
 

jo15765

Registered User.
Local time
Today, 13:35
Joined
Jun 24, 2011
Messages
130
stepping through the code

lastrow = 1728
lColumn = 1

then it hits the line
Code:
    strValue = Right(strValue, Len(strValue)-1)

And gives the error
Run-time error '5':
Invalid procedure call or argument

It hits the line
Code:
For x = 2 To lColumn

And immediately jumps to the line above in the first code block and gives the error
 

Isaac

Lifelong Learner
Local time
Today, 13:35
Joined
Mar 14, 2017
Messages
8,869
While it is stopped and yellow highlighting that line, click View>Immediate Window (if not already shown), and ask the immediate window some relevant debugging questions, like:
?StrValue [then hit Enter]
?lColumn [then hit Enter]

let me know what it says ...
 

jo15765

Registered User.
Local time
Today, 13:35
Joined
Jun 24, 2011
Messages
130
Attached is what it shows
 

Attachments

  • 1234.jpg
    1234.jpg
    7.9 KB · Views: 143

Isaac

Lifelong Learner
Local time
Today, 13:35
Joined
Mar 14, 2017
Messages
8,869
Interesting. So the problem is that lColumn isn't working as we'd expect. Are you sure that there are values (headers) in row 1 ?? Can you post your current code?
 

Isaac

Lifelong Learner
Local time
Today, 13:35
Joined
Mar 14, 2017
Messages
8,869
Aww crap. I made a mistake, one second, I'll post a corrected line
 

Isaac

Lifelong Learner
Local time
Today, 13:35
Joined
Mar 14, 2017
Messages
8,869
Change
Code:
lColumn = ws.range("A" & ws.columns.count).end(-4159).column
to
Code:
lColumn = ws.range("XFD1").end(-4159).column
 

jo15765

Registered User.
Local time
Today, 13:35
Joined
Jun 24, 2011
Messages
130
Yes row 1 has headers. Only change I made was I removed a duplicate variable declaration from your code, and changed the For line so it was valid syntax

Code:
Function Test()
Dim wb As Excel.Workbook
Dim xlApp As Excel.Application
Dim x As Long
Dim strValue As String
Dim rng As Object, ws As Object, y As Long
Dim lastrow As Long, lColumn As Long

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False

Set wb = xlApp.Workbooks.Open("C:\Test\Test.xlsx", False, False)
Set ws = wb.Sheets(1)


lastrow = ws.Range("A" & ws.Rows.Count).End(-4162).Row
lColumn = ws.Range("A" & ws.Columns.Count).End(-4159).Column
For y = 2 To lastrow
    strValue = ""
   For x = 2 To lColumn
        strValue = strValue & "." & ws.Cells(y, x)
   Next x
    strValue = Right(strValue, Len(strValue) - 1)
    ws.Cells(y, lColumn + 1).Value = strValue
Next y

wb.Save
wb.Close
xlApp.DisplayAlerts = False
xlApp.Quit

End Function
 

Isaac

Lifelong Learner
Local time
Today, 13:35
Joined
Mar 14, 2017
Messages
8,869
ok thanks, see my previous post about the change
 

Users who are viewing this thread

Top Bottom