Input data at blank row at certain columns

ChanL

Board Regular
Joined
Apr 8, 2021
Messages
65
Office Version
  1. 2019
Platform
  1. Windows
Hi, I want my VBA to run like this
1. copy data from sheet "fromtext" to the table in sheet "report" (everytime the data will copy to the last row of the table)
2. After copy the data, the table has two extra column "year" and "date" which require user manually input year and month of the copied data

my problem is at step 2, which I don't know what is the best and also the simplest way to automate manual year and month input

this is my code , please advice

VBA Code:
With Sheets ("fromtext")
  . range("A9",.range("A9").end(xlDown).End(xltoright).copy
end with

With sheets("report").listobjects(1).range
   .cells(.rows.count+1,1).pastespecial xlvalues
end with

application.cutcopymode=false
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
my problem is at step 2, which I don't know what is the best and also the simplest way to automate manual year and month input
Try using an InputBox to prompt the user for those values, i.e.
VBA Code:
Dim yr as Long
yr = InputBox("Please enter the year")
You can then populate the cells that you want with that value.

If you need help with that, we will need more information, i.e. where these values are supposed to go.
Posting some images of what the data and tables look like would go a long way in helping us see that.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Hi, thanks so much for reply! I also think of input box but I google around still can't find a good way for me to write a code for it.
Here I include a mini sheet for my sample data.
So what I want is, after the VBA run to copy and paste the data to the table, the column for year and month will blank correspond to the new rows pasted. And here, if possible as you say, it would be great if VBA can prompt an input box for user to input year and month to it.

Book3.xlsx
ABCDEFGHIJKL
1Order IDShip DateOrder DateShip ModeCustomer IDCustomer NameDestinationContact NumberStatusSegmentYearMonth
2CA-2014-10589311/18/201411/11/2014Standard ClassPK-19075Pete KrizNew York123456On the WayConsumer
3CA-2014-1671645/15/20145/13/2014Second ClassAG-10270Alejandro GroveWashington123456FailedConsumer
4CA-2014-1433369/1/20148/27/2014Second ClassZD-21925Zuschuss DonatelliNew Zealand123456DeliveredConsumer
5CA-2014-1433369/1/20148/27/2014Second ClassZD-21925Zuschuss DonatelliNew York123456On the WayConsumer
6CA-2014-1433369/1/20148/27/2014Second ClassZD-21925Zuschuss DonatelliWashington123456DeliveredConsumer
7CA-2016-13733012/13/201612/9/2016Standard ClassKB-16585Ken BlackJapan123456On the WayCorporate
8CA-2016-13733012/13/201612/9/2016Standard ClassKB-16585Ken BlackNew York123456DeliveredCorporate
9US-2017-1569097/18/20177/16/2017Second ClassSF-20065Sandra FlanaganWashington123456On the WayConsumer
10CA-2015-1063209/30/20159/25/2015Standard ClassEB-13870Emily BurnsNew Zealand123456On the WayConsumer
11CA-2016-1217551/20/20161/16/2016Second ClassEH-13945Eric HoffmannNew York123456FailedConsumer
12CA-2016-1217551/20/20161/16/2016Second ClassEH-13945Eric HoffmannWashington123456On the WayConsumer
13US-2015-1506309/21/20159/17/2015Standard ClassTB-21520Tracy BlumsteinNew Zealand123456DeliveredConsumer
14US-2015-1506309/21/20159/17/2015Standard ClassTB-21520Tracy BlumsteinNew York123456On the WayConsumer
15US-2015-1506309/21/20159/17/2015Standard ClassTB-21520Tracy BlumsteinWashington123456DeliveredConsumer
Sheet1
Try using an InputBox to prompt the user for those values, i.e.
VBA Code:
Dim yr as Long
yr = InputBox("Please enter the year")
You can then populate the cells that you want with that value.

If you need help with that, we will need more information, i.e. where these values are supposed to go.
Posting some images of what the data and tables look like would go a long way in helping us see that.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Using the concepts I gave you in the last post, try something like this:
VBA Code:
Sub InsertYearMonth()

    Dim yr As Long
    Dim mo As Long
   
    Dim fr As Long
    Dim lr As Long
   
'   Find first row needing data in column K
    fr = Sheets("report").Cells(Rows.Count, "K").End(xlUp).Row + 1
   
'   Find last row needing data (using column A)
    lr = Sheets("report").Cells(Rows.Count, "A").End(xlUp).Row
   
'   Prompt for year and month
    yr = InputBox("Please enter the year")
    mo = InputBox("Please enter the month")
   
'   Populate values into columns K and L
    Sheets("report").Activate
    Range(Cells(fr, "K"), Cells(lr, "K")) = yr
    Range(Cells(fr, "L"), Cells(lr, "L")) = mo

End Sub
 
Upvote 0
Using the concepts I gave you in the last post, try something like this:
VBA Code:
Sub InsertYearMonth()

    Dim yr As Long
    Dim mo As Long
  
    Dim fr As Long
    Dim lr As Long
  
'   Find first row needing data in column K
    fr = Sheets("report").Cells(Rows.Count, "K").End(xlUp).Row + 1
  
'   Find last row needing data (using column A)
    lr = Sheets("report").Cells(Rows.Count, "A").End(xlUp).Row
 
'   Prompt for year and month
    yr = InputBox("Please enter the year")
    mo = InputBox("Please enter the month")
  
'   Populate values into columns K and L
    Sheets("report").Activate
    Range(Cells(fr, "K"), Cells(lr, "K")) = yr
    Range(Cells(fr, "L"), Cells(lr, "L")) = mo

End Sub
Hi thanks for the reply, i modify your code to fit in the WITH loop in my vba
VBA Code:
dim nextrow as long, year as long, month as string, fr as long, lr as long
With Sheets("Sheet1").ListObjects(1).Range
  If .SpecialCells(xlConstants).Count = .Columns.Count Then
    nextrow = 2
  Else
    nextrow = .Rows.Count + 1
  End If
  .Cells(nextrow, 1).PasteSpecial xlValues

   fr = .Cells(Rows.Count, "K").End(xlUp).Row + 1
   lr = .Cells(Rows.Count, "A").End(xlUp).Row
 
year = inputbox("Enter the year")
month=inputbox("Enter the month")
.Range(Cells(fr, "K"), Cells(lr, "K")) = year
.Range(Cells(fr, "L"), Cells(lr, "L")) = month

and I hit application-defined error at
VBA Code:
.Range(Cells(fr, "K"), Cells(lr, "K")) = year
.Range(Cells(fr, "L"), Cells(lr, "L")) = month
 
Upvote 0
Never use reserved words like "year" and "month" as the name of your variables!
Reserved words are names of existing functions, properties, methods, etc ("year" and "month" are names of existing Excel and VBA functions).
Using reserved words as the names of variables, procedures, or functions can cause errors and unexpected behavior.

That is why I used abbreviations in my example.
One thing I do if I am unsure if something is a reserved word or not is to prefix it with "my", i.e. "myMonth", "myYear", etc.
 
Upvote 0
Never use reserved words like "year" and "month" as the name of your variables!
Reserved words are names of existing functions, properties, methods, etc ("year" and "month" are names of existing Excel and VBA functions).
Using reserved words as the names of variables, procedures, or functions can cause errors and unexpected behavior.

That is why I used abbreviations in my example.
One thing I do if I am unsure if something is a reserved word or not is to prefix it with "my", i.e. "myMonth", "myYear", etc.
thanks for the advice. So, i changed the code
VBA Code:
.Range(Cells(fr, "K"), Cells(lr, "K")) = year
.Range(Cells(fr, "L"), Cells(lr, "L")) = month

to
VBA Code:
.Range(.Cells(fr, "K"), .Cells(lr, "K")) = yr
.Range(.Cells(fr, "L"), .Cells(lr, "L")) = mth

now the code doesn't hit the error, but the result is not as expected.
the code only insert the data from input box to the last row of column K and also the one of the row after it which does not contain data for column A to J.
 
Upvote 0
the code only insert the data from input box to the last row of column K and also the one of the row after it which does not contain data for column A to J.
Please post the following I can try to recreate your scenario:
1. The data set you are testing this on, before you run the code.
2. What the "report" sheet looks like, before you run the code.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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