Append Data with Custom Hashtag, Automate with VBA & Button

ThePianoman

New Member
Joined
Mar 15, 2011
Messages
15
I'm looking for a bit of help for a pretty straightforward small project. I'm fairly familiar with all the basic functions of excel, VLOOKUP, pivot tables, etc., but clueless when it comes to VBA. I realize it can probably be done manually pretty easily, or using helper columns or similar, but I will need to perform the function several times and make it easy for other research study sites to use. So automated with VBA is the way I want to go. I'm willing to donate a small fee to whoever can work with me on this- you guys are the experts.

Here's a brief rundown:


  • I will periodically export a dataset from a research study database, in excel format. It will look something like this (with exactly this many columns, but potentially many more rows of data):

ABCDEFGH
1
demo_mrn

<tbody>
</tbody>
ffi_shrinking_score

<tbody>
</tbody>
ffi_gripstrength_score

<tbody>
</tbody>
ffi_slowwalking_score

<tbody>
</tbody>
ffi_lowactivity_score

<tbody>
</tbody>
ffi_fr

<tbody>
</tbody>
cfs_cfsscore

<tbody>
</tbody>
rai_score_total

<tbody>
</tbody>
21111111101001189
32222222232213455
43333333312110367
54444444411114142
65555555500122225
76666666631213467
87777777741332184
98888888814322177

<tbody>
</tbody>


  • The data (B2:H9) needs to have "#F4_[column header in row 1]_" appended to it so that it will look exactly like this:

ABCDEFGH
1
demo_mrn

<tbody>
</tbody>
ffi_shrinking_score

<tbody>
</tbody>
ffi_gripstrength_score

<tbody>
</tbody>
ffi_slowwalking_score

<tbody>
</tbody>
ffi_lowactivity_score

<tbody>
</tbody>
ffi_fr

<tbody>
</tbody>
cfs_cfsscore

<tbody>
</tbody>
rai_score_total

<tbody>
</tbody>
211111111#F4_ffi_shrinking_score_0#F4_ffi_gripstrength_score_1#F4_ffi_slowwalking_score_0#F4_ffi_lowactivity_score_0#F4_ffi_fr_1#F4_cfs_cfsscore_1#F4_rai_score_total_89
322222222#F4_ffi_shrinking_score_3#F4_ffi_gripstrength_score_2#F4_ffi_slowwalking_score_2#F4_ffi_lowactivity_score_1#F4_ffi_fr_3#F4_cfs_cfsscore_4#F4_rai_score_total_55
433333333#F4_ffi_shrinking_score_1#F4_ffi_gripstrength_score_2#F4_ffi_slowwalking_score_1#F4_ffi_lowactivity_score_1#F4_ffi_fr_0#F4_cfs_cfsscore_3#F4_rai_score_total_67
544444444#F4_ffi_shrinking_score_1#F4_ffi_gripstrength_score_1#F4_ffi_slowwalking_score_1#F4_ffi_lowactivity_score_1#F4_ffi_fr_4#F4_cfs_cfsscore_1#F4_rai_score_total_42
655555555#F4_ffi_shrinking_score_0#F4_ffi_gripstrength_score_0#F4_ffi_slowwalking_score_1#F4_ffi_lowactivity_score_2#F4_ffi_fr_2#F4_cfs_cfsscore_2#F4_rai_score_total_25
766666666#F4_ffi_shrinking_score_3#F4_ffi_gripstrength_score_1#F4_ffi_slowwalking_score_2#F4_ffi_lowactivity_score_1#F4_ffi_fr_3#F4_cfs_cfsscore_4#F4_rai_score_total_67
877777777#F4_ffi_shrinking_score_4#F4_ffi_gripstrength_score_1#F4_ffi_slowwalking_score_3#F4_ffi_lowactivity_score_3#F4_ffi_fr_2#F4_cfs_cfsscore_1#F4_rai_score_total_84
988888888#F4_ffi_shrinking_score_1#F4_ffi_gripstrength_score_4#F4_ffi_slowwalking_score_3#F4_ffi_lowactivity_score_2#F4_ffi_fr_2#F4_cfs_cfsscore_1#F4_rai_score_total_77

<tbody>
</tbody>


  • The dataset could include up to 1500 rows. Preferably I would like the VBA function to be initiated with a button. So it would work like this:

  1. Export the data to excel.
  2. Copy/Paste data into the macro worksheet
  3. Click the button to convert/append the data
  4. Click another button to create a new workbook (regular excel file), and copy the converted data into it.

Thanks in advance for your help.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I'm assuming that the number of columns is fixed. If so, your first macro is:
Code:
Sub AppendMacro()
With ActiveSheet
LastRow = .Cells.SpecialCells(xlCellTypeLastCell).Row
Application.ScreenUpdating = False
For DataCol = 2 to 8
  For DataRow = 2 to LastRow
    .Cells(DataRow, DataCol).Value = "#F4_" & .Cells(1, DataCol).Value & "_" & .Cells(DataRow, DataCol).Value
  Next
Next
Application.ScreenUpdating = True
End With
End Sub
The macro file will be a .xlsm file. The following macro will save it as a .xlsx file with the same name:
Code:
Sub SaveXlsx()
fName = ActiveWorkbook.Path & "\" & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5) & ".xlsx"
ActiveWorkbook.SaveAs(fName)
End Sub
 
Last edited:
Upvote 0

Thanks Trevor, this is great. I do get the following error:

"Run-time error '1004': This extension can not be used with the selected file type. Change the file extension in the File name text box or select a different file type by changing the Save as type"

I would actually prefer to have the worksheet copied to a new workbook, and the "save as" prompt to appear so I can choose location and name of the new file. Is that doable?

Thanks.
 
Upvote 0
Try this for the second macro:
Code:
Sub SaveXlsx()
Dim ThisWb As Workbook
Dim NewWb As Workbook
Set ThisWb = ActiveWorkbook
Set NewWb = Workbooks.New
ThisWb.ActiveSheet.Range("A:H").Copy
NewWb.ActiveSheet.Range("A:H").Select
Selection.Paste
fName = Application.GetSaveAsFilename(,"Excel Workbook (*.xlsx), *.xlsx")
If fName = "" then
  'User cancelled, do not save file
Else
  NewWb.SaveAs(fName)
End If
End Sub
 
Last edited:
Upvote 0
Thanks Trevor- I had to modify it just a bit, but it worked. Please pm me your Paypal email address. Thanks!
 
Upvote 0
Glad that it worked.
Its a free forum! I've learned a lot from it over the years, so I'm always happy to answer any threads that I can.
 
Upvote 0
Well thanks! I feel like I'm robbing you of your knowledge. I appreciate the help, I wish I knew a bit more VBA to to do this on my own.
 
Upvote 0

Forum statistics

Threads
1,215,161
Messages
6,123,371
Members
449,097
Latest member
thnirmitha

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