VB help please.

shinseijutsu

New Member
Joined
Jul 25, 2011
Messages
5
I am new to using VB, but I have been told it's the best way to go with what I am trying to do. I hope this is easy to do.

I have a front sheet which is used as an input form, this sheet will have two columns, column A has a number in each cell and column B has something like "B4" in it.
I would like to be able to click a button and the script runs and puts the value that is in A1 in to the cell that is written in B1 (on a different sheet), and so on for the rest of the data.

I hope this makes sense, I will try and add an example if needed.

Thank you so much in advance if you can help.

Kindest regards
Richard
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
What do you want to happen to the values in the front sheet? Are they cleared after the A-column value has been copied to the second sheet?

Or are they marked in some way - ticked or highlighted, perhaps, to indicate that they've been done?
 
Upvote 0
They would be cleared. The front page is just for inputting the data and the second sheet stores it.

I hope this makes sense.
 
Upvote 0
So Sheet 'input' would look like:

A B

8 C1
8 C6
Y C4
F C17
5 C96


And when a button is clicked:

8 would be put in to cell C1 on sheet 'accounts'
8 would be put in to cell C6 on sheet 'accounts'
Y would be put in to cell C4
F in cell C17
and 5 in cell C96 all on sheet 'accounts'

This sheet could then be blanked, new data entered and the button clicked again for new data to be put in to the 'accounts' sheet.
 
Upvote 0
Try this. Create a new workbook for this purpose and make sure there are two worksheets in it. Press Alt-F11 to open Microsoft Visual Basic, press Ctrl-R to view the Project Explorer, then go Insert > Module. A new 'standard' code module will appear under Modules, probably called Module1. You might need to click the + symbol against Modules to expand the group.

Double-click the name of this new module to open it. Remove any code you find in the code window (probably only the words Option Explicit, if anything) and paste my code in its place - everything from Option Explicit down to and including End Sub:-

Code:
[FONT=Fixedsys]Option Explicit[/FONT]
[FONT=Fixedsys][/FONT] 
[FONT=Fixedsys]Public Sub MoveValues()[/FONT]
[FONT=Fixedsys][/FONT] 
[FONT=Fixedsys]  Dim ws1 As Worksheet
  Dim ws2 As Worksheet
  Dim iLastRow As Long
  Dim iRow As Long
  Dim rTarget As Range
  
  Set ws1 = ThisWorkbook.Sheets(1)
  Set ws2 = ThisWorkbook.Sheets(2)
  
  iLastRow = Application.WorksheetFunction.Max( _
             ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row, _
             ws1.Cells(ws1.Rows.Count, "B").End(xlUp).Row)
  
  For iRow = 1 To iLastRow
    Select Case True
      Case IsEmpty(ws1.Cells(iRow, "A")) And IsEmpty(ws1.Cells(iRow, "B"))
        ' no data - ignore this row
      Case IsEmpty(ws1.Cells(iRow, "A")) And Not IsEmpty(ws1.Cells(iRow, "B"))
        ws1.Cells(iRow, "C") = "No value to copy"
      Case Not IsEmpty(ws1.Cells(iRow, "A")) And IsEmpty(ws1.Cells(iRow, "B"))
        ws1.Cells(iRow, "C") = "Target cell address missing"
      Case Else
        Set rTarget = Nothing
        On Error Resume Next
        Set rTarget = ws2.Range(ws1.Cells(iRow, "B").Value)
        On Error GoTo 0
        If Not rTarget Is Nothing Then
          ws1.Cells(iRow, "A").Copy Destination:=rTarget
          ws1.Range("A" & iRow & ":C" & iRow).ClearContents
        Else
          ws1.Cells(iRow, "C") = "Invalid target cell address"
        End If
    End Select
  Next iRow
  
End Sub
[/FONT]

Return to your worksheet and make sure you're in Sheet1. Click Developer > Insert (the toolbox with the crossed hammer & spanner), and select the first Form control (tooltip "Button" if you mouse over it). Click-drag-release to place it on your worksheet - don't worry too much about size, shape and location as you can modify these later), then select the MoveValues macro from the list and click OK. There will be a grey 'design mode' frame around the button: click out of the button on to the worksheet to make this disappear.

Still in Sheet1, enter some values in column A and some cell addresses in column B. Include one value with no cell address, one value with an invalid cell address and a cell address with no value. Click the button.

One of three things will happens:-
  • Nothing at all
  • VBA reports an error
  • The values in the valid rows are copied on to Sheet2 and the ones on the invalid rows show an error message
Come back and tell me which.
 
Upvote 0
Wow!

That seems to work really well!!

Amazing! I don't think I would have managed this at all.

If I wanted it to leave the data in and highlight the rows that worked what would I need to change? I am thinking now that that may be safer so that I can check my data entry and then delete when I am ready.

Thank you again, this is superb!
 
Upvote 0
Try this:-
Code:
[FONT=Fixedsys]Option Explicit[/FONT]
 
[FONT=Fixedsys]Public Sub MoveValues()[/FONT]
 
[FONT=Fixedsys] Dim ws1 As Worksheet[/FONT]
[FONT=Fixedsys] Dim ws2 As Worksheet[/FONT]
[FONT=Fixedsys] Dim iLastRow As Long[/FONT]
[FONT=Fixedsys] Dim iRow As Long[/FONT]
[FONT=Fixedsys] Dim rTarget As Range[/FONT]
 
[FONT=Fixedsys] Set ws1 = ThisWorkbook.Sheets(1)[/FONT]
[FONT=Fixedsys] Set ws2 = ThisWorkbook.Sheets(2)[/FONT]
 
[FONT=Fixedsys] iLastRow = Application.WorksheetFunction.Max( _[/FONT]
[FONT=Fixedsys][FONT=Fixedsys]            ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row, _[/FONT]
[/FONT][FONT=Fixedsys][FONT=Fixedsys]            ws1.Cells(ws1.Rows.Count, "B").End(xlUp).Row, _[/FONT]
            ws1.Cells(ws1.Rows.Count, "C").End(xlUp).Row)[/FONT]
 
[FONT=Fixedsys] For iRow = 1 To iLastRow[/FONT]
[FONT=Fixedsys]   Select Case True[/FONT]
[FONT=Fixedsys]     Case IsEmpty(ws1.Cells(iRow, "A")) And IsEmpty(ws1.Cells(iRow, "B"))[/FONT]
[FONT=Fixedsys]       [COLOR=blue]ws1.Range("A" & iRow & ":B" & iRow).Interior.Color = xlNone[/COLOR][/FONT]
[FONT=Fixedsys]       ws1.Range("C" & iRow).ClearContents
     Case IsEmpty(ws1.Cells(iRow, "A")) And Not IsEmpty(ws1.Cells(iRow, "B"))[/FONT]
[FONT=Fixedsys]       ws1.Cells(iRow, "C") = "No value to copy"[/FONT]
[FONT=Fixedsys]       [COLOR=blue]ws1.[/COLOR][COLOR=blue]Range("A" & iRow & ":B" & iRow).Interior.Color = vbRed[/COLOR][/FONT]
[FONT=Fixedsys]     Case Not IsEmpty(ws1.Cells(iRow, "A")) And IsEmpty(ws1.Cells(iRow, "B"))[/FONT]
[FONT=Fixedsys]       ws1.Cells(iRow, "C") = "Target cell address missing"[/FONT]
[FONT=Fixedsys]       [COLOR=blue]ws1.Range("A" & iRow & ":B" & iRow).Interior.Color = vbRed[/COLOR][/FONT]
[FONT=Fixedsys]     Case Else[/FONT]
[FONT=Fixedsys]       Set rTarget = Nothing[/FONT]
[FONT=Fixedsys]       On Error Resume Next[/FONT]
[FONT=Fixedsys]       Set rTarget = ws2.Range(ws1.Cells(iRow, "B").Value)[/FONT]
[FONT=Fixedsys]       On Error GoTo 0[/FONT]
[FONT=Fixedsys]       If Not rTarget Is Nothing Then[/FONT]
[FONT=Fixedsys]         ws1.Cells(iRow, "A").Copy Destination:=rTarget[/FONT]
[FONT=Fixedsys]         [COLOR=blue]ws1.Range("A" & iRow & ":B" & iRow).Interior.Color = vbGreen[/COLOR][/FONT]
[FONT=Fixedsys]         ws1.Range("C" & iRow).ClearContents[/FONT]
[FONT=Fixedsys]       Else[/FONT]
[FONT=Fixedsys]         ws1.Cells(iRow, "C") = "Invalid target cell address"[/FONT]
[FONT=Fixedsys]         [COLOR=blue]ws1.Range("A" & iRow & ":B" & iRow).Interior.Color = vbRed[/COLOR][/FONT]
[FONT=Fixedsys]       End If[/FONT]
[FONT=Fixedsys]   End Select[/FONT]
[FONT=Fixedsys] Next iRow[/FONT]
 
[FONT=Fixedsys]End Sub[/FONT]
I've highlighted the main changes in blue but there are other minor changes, so remove all of the old code and paste this in its place.
 
Upvote 0
Well, thank you for the compliment but I wouldn't like you to think that this code was in any way out of the ordinary. In fact it uses a very small subset of the facilities which VBA offers and actually does very little processing, none of it particularly complex. As you can see, the first version took just twenty-ish minutes to design, code and test, and I dare say that if you read through it slowly, you'd be able to identify without too much difficulty exactly which bits were doing what.

With a little bit of effort - probably not much - you could be writing VBA to do similar sorts of things, and this forum is an excellent place to learn how to do that. Its participants are very knowledgeable and experienced - many more so than I - and will be able to help you with anything you undertake, I'm sure.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,282
Members
452,902
Latest member
Knuddeluff

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