Starting macros

concreteinterface

Board Regular
Joined
Jul 10, 2008
Messages
137
I would like to start screwing around with macros. I've started a book with 2 sheets. If there is a number entered into D2:D21 on Sheet1 it will copy the entire row (A3:K3), move it to Sheet2, paste it, move down one line, go back to Sheet1, check for other rows, and paste them if needed.

Does anyone have a sample of code I can just start experimenting around with?
 

RalphA

Well-known Member
Joined
May 14, 2003
Messages
3,829
Creat the code you need, using Tools, Macro, Record new Macro. Then, click on Tools, Macro, Macros..,. choose the macro, and click on Edit to change it. Or, go to a sheet tab, do a right click, choose View code, and edit ihe macro you just rote. If you need more help. post again here.
 

d0wnt0wn

Well-known Member
Joined
Oct 28, 2002
Messages
747
in excel 2007 click on view and then macros... then record macro...... do a few copy paste things then stop recording then click on macros....view macros... then click on edit and the code will appear....alternately you can right click on your worksheet and select "view code" on the left window pane select modules and you can look at your code that way...... microsoft will show every single step that you did there and often you do not need some of it but you can always post your macro here and someone can clean it up for you

Hope that helps
 

d0wnt0wn

Well-known Member
Joined
Oct 28, 2002
Messages
747
Creat the code you need, using Tools, Macro, Record new Macro. Then, click on Tools, Macro, Macros..,. choose the macro, and click on Edit to change it. Or, go to a sheet tab, do a right click, choose View code, and edit ihe macro you just rote. If you need more help. post again here.
heh you beat me to it ralph
 

concreteinterface

Board Regular
Joined
Jul 10, 2008
Messages
137
Sub Macro1()
'
' Macro1 Macro
'

'
Range("A2:K2").Select
Selection.Copy
Sheets("Sheet2").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("A7:K7").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet2").Select
Range("A2").Select
ActiveSheet.Paste
End Sub

OK I've got that part down and I can see what the code is doing and I do understand it (except Application.CutCopyMode = False). Now I need to type in an IF statement to say

IF cell D2 has any value OR if no value move to check E2
Range("A2:K2").Select
Selection.Copy
Sheets("Sheet2").Select
ActiveSheet.Paste
Move down one row to A2
Sheets("Sheet1").Select
Move on to check E2 and repeat
 

RalphA

Well-known Member
Joined
May 14, 2003
Messages
3,829
Now, that requires mor knowledge of VBA than Ihaave! Hopefully, someone else will pick this up from here. good luck!
 

Joe Was

MrExcel MVP
Joined
Feb 19, 2002
Messages
7,539
The best way to learn, is to use the boards search utility, and search the board before you post. Play with the code you find, see if between the boards samples and the help in Excel, if you can make some progress and when you really get stuck then post for help showing your code and indicating the one small portion or general idea you have that you just can't get.

Think about the problem and the steps it will take to make it happen. From your post you have not done this yet, and from the recorded code you posted, you are missing steps.

To copy to a sheet you need to have that sheet first, do you want to name a new sheet using an InPutBox to get user input about it or do you want it to be automatically named in some way, using the default name or a value found on the master sheet or by some algorithm. First decide how to start, approach the problem in steps, make progress step by step. Very little ever happens all at once. Decide what needs to happen first then what must happen next, untill the problem is finished.

Sub myNewList()
'Standard module code, like: Module1.
Dim lngLstRow&, lngMyshtsCnt&, lngNewLstRow&
Dim rngMyData As Object
Dim strMyNewShtNm$

'Count the sheets in the Workbook.
lngMyshtsCnt = Worksheets.Count

With Sheets.Add
.Move After:=Sheets(lngMyshtsCnt + 1)
.Name = "myList" & lngMyshtsCnt + 1
End With

strMyNewShtNm = ActiveSheet.Name

'Count the used rows in column D, AKA: 4 on Sheet1.
lngLstRow = Sheets("Sheet1").Cells(Rows.Count, 4).End(xlUp).Row

'Define Sheet1's data range!
Set rngMyData = Sheets("Sheet1").Range("D2:D" & lngLstRow)

'Get starting row on new sheet!
lngNewLstRow = Sheets(strMyNewShtNm).Cells(Rows.Count, 4).End(xlUp).Row + 1

'Get data to move!
For Each Cell In rngMyData

'Test for good data and copy it to the new sheet!
If Cell.Value <> "" Then
lngNewLstRow = lngNewLstRow + 1
Sheets(strMyNewShtNm).Range("A" & lngNewLstRow).Value = Cell.Value
End If

Next Cell
End Sub
 
Last edited:

concreteinterface

Board Regular
Joined
Jul 10, 2008
Messages
137
The best way to learn, is to use the boards search utility, and search the board before you post. Play with the code you find, see if between the boards samples and the help in Excel, if you can make some progress and when you really get stuck then post for help showing your code and indicating the one small portion or general idea you have that you just can't get.

Think about the problem and the steps it will take to make it happen. From your post you have not done this yet, and from the recorded code you posted, you are missing steps.

To copy to a sheet you need to have that sheet first, do you want to name a new sheet using an InPutBox to get user input about it or do you want it to be automatically named in some way, using the default name or usinf a value found on the master sheet or by some algorithm. First decide how to start, approach the problem in steps, make progress step by step. Very little ever happens all at once. Decide what needs to happen first then what must happen next, untill the promlem is finished.

Sub myNewList()
'Standard module code, like: Module1.
Dim lngLstRow&, lngMyshtsCnt&, lngNewLstRow&
Dim rngMyData As Object
Dim strMyNewShtNm$

'Count the sheets in the Workbook.
lngMyshtsCnt = Worksheets.Count

With Sheets.Add
.Move After:=Sheets(lngMyshtsCnt + 1)
.Name = "myList" & lngMyshtsCnt + 1
End With

strMyNewShtNm = ActiveSheet.Name

'Count the used rows in column D, AKA: 4 on Sheet1.
lngLstRow = Sheets("Sheet1").Cells(Rows.Count, 4).End(xlUp).Row

'Define Sheet1's data range!
Set rngMyData = Sheets("Sheet1").Range("D2:D" & lngLstRow)

'Get starting row on new sheet!
lngNewLstRow = Sheets(strMyNewShtNm).Cells(Rows.Count, 4).End(xlUp).Row + 1

'Get data to move!
For Each Cell In rngMyData

'Test fot good data and copy it to the new sheet!
If Cell.Value <> "" Then
lngNewLstRow = lngNewLstRow + 1
Sheets(strMyNewShtNm).Range("A" & lngNewLstRow).Value = Cell.Value
End If

Next Cell
End Sub

I thank you for the help and I will try to understand the code...

I do not agree with the first part of your statement.. I am a Excel beginner not a whiz kid.. All people are different and all people learn differently. When I have a problem I call tech support, have a discussion with people who are savvy on their product, and I learn from them so I don't have to call them the next time I have a similar problem. Searching through a message board for hours on end only to confuse me further is not my thing... Apologies.
 

Joe Was

MrExcel MVP
Joined
Feb 19, 2002
Messages
7,539
I agree with you and have had to approach the same problem in different ways for different people, you the reasons you stated. This problem in case, you could have solved it in any number of ways and if you look at the other posts, you will find that indeed their are many ways to do this. One of those way may have been along the lines you think. I am merely offering you tools and a structure that can be applied to many problems, which is the real way to learn, trasportability is the key.

The old axiom, you can give a man a fish to eat and he will not be hungry now, or you can teach him how to fish and he will never be hungry, is true to a large extent.

Keep at your code work. Play with the code see what can and can't be done. Programming is learning to speak in a foreign language and in many cases to solve puzzles or mysteries with the information available. The trick is to have a tool set and a system or method, know the steps to take is a big part. If the actual experience proves you have missed as step or need to try a different method, do it, explore that is how you learn to program no matter how you learn other things.
 

Forum statistics

Threads
1,078,499
Messages
5,340,740
Members
399,393
Latest member
farlow

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top