I have data in three columns, A, B and C.
I want to copy the following 3 values from column C if there is a match between column A and B. For example, I would like to copy number 1,3 and 6 from column C because A and B match in third row.
A B C
1 2 4
3 4 4
5 5 1
4 6 3
4 8 6
1 8 3
I have tried Resize, Range(Cells(Selection.Row, 1), Cells(Selection.Row, 3)).Copy etc. but nothing seem to work.
Sub test()
Dim rngsize As Range, rngsize2 As Range, rngmake As Range, rngmake2 As Range, rngprice As Range, rngprice2 as range, i As Integer, j As Integer, x As Integer
x = 3
For i = 2 To Sheets("Sheet3").Range("E" & Rows.Count).End(xlUp).Row
For j = 7 To Sheets("Sheet2").Range("E" & Rows.Count).End(xlUp).Row
Set rngsize = Sheets("Sheet3").Range("E" & i)
Set rngsize2 = Sheets("Sheet2").Range("E" & j)
Set rngmake = Sheets("Sheet3").Range("F" & i)
Set rngmake2 = Sheets("Sheet2").Range("F" & j)
Set rngprice = Sheets("Sheet3").Range("X" & i)
Set rngprice2 = Sheets("Sheet2").Range("X" & j)
If rngsize * 0.5 <= rngsize And rngsize2 + 1.5 >= rngsize Then
If rngmake2 * 0.5 <= rngmake And rngmake2 * 1.5 >= rngmake Then
Range(Cells(Selection.Row, 1), Cells(Selection.Row, 3)).Copy
rngprice2.Copy
Worksheets("Sheet4").Range("F" & x).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = xlCopy
x = x + 1
End If
End If
Next j
Next i
End Sub
I could not follow your code so I threw something together based on your example and description. You will have to modify the constants and worksheets to fit your application.
From your description and example you want VBA for:
When A matches B in the same row, copy C from that row and C from the next 2 rows to another worksheet.
Private Sub CopyMatch()
Dim i As Integer
Dim j As Integer
Dim wsCopy As Worksheet
Dim wsPaste As Worksheet
Const intACol As Integer = 1
Const intBCol As Integer = 2
Const intCCol As Integer = 3
Const intPasteCol As Integer = 1
Const intCopyRowStart As Integer = 2
Const intPasteRowStart As Integer = 1
'assign worksheets
Set wsCopy = Sheets("Sheet1")
Set wsPaste = Sheets("Sheet2")
'cycle through each row
i = intCopyRowStart
j = intPasteRowStart
Do Until wsCopy.Cells(i, intACol).Value = "" And _
wsCopy.Cells(i, intBCol).Value = "" And _
wsCopy.Cells(i, intCCol).Value = ""
'check for A-B match
If wsCopy.Cells(i, intACol).Value = wsCopy.Cells(i, intBCol).Value Then
'copy C value from match row + 2 next rows for C
wsCopy.Range(Cells(i, intCCol), Cells(i + 2, intCCol)).Copy
'paste in other sheet
wsPaste.Cells(j, intPasteCol).PasteSpecial Paste:=xlPasteValues
j = j + 3
End If
i = i + 1
Loop
End Sub
This returned the values 1,3, & 6 in another sheet.
My attempt to apply this to your code is as follows:
Sub test()
Dim rngsize As Range, rngsize2 As Range, rngmake As Range, rngmake2 As Range, rngprice As Range, rngprice2 As Range, i As Integer, j As Integer, x As Integer
x = 3
For i = 2 To Sheets("Sheet3").Range("E" & Rows.Count).End(xlUp).Row
For j = 7 To Sheets("Sheet2").Range("E" & Rows.Count).End(xlUp).Row
Set rngsize = Sheets("Sheet3").Range("E" & i)
Set rngsize2 = Sheets("Sheet2").Range("E" & j)
Set rngmake = Sheets("Sheet3").Range("F" & i)
Set rngmake2 = Sheets("Sheet2").Range("F" & j)
Set rngprice = Sheets("Sheet3").Range("X" & i)
Set rngprice2 = Sheets("Sheet2").Range("X" & j)
If rngsize * 0.5 <= rngsize And rngsize2 + 1.5 >= rngsize Then
If rngmake2 * 0.5 <= rngmake And rngmake2 * 1.5 >= rngmake Then
Sheets("Sheet2").Range(Cells(rngprice.Row, rngprice.Column), Cells(rngprice.Row + 2, rngprice.Column)).Copy
Sheets("Sheet4").Range("F" & x).PasteSpecial Paste:=xlPasteValues
x = x + 3
End If
End If
Next j
Next i
End Sub
It runs, not sure if it works as intended though.
Related
I have this table about 50,000 rows long that I would like Excel to go through and assign a number or letter.
Basically I am trying to group rows of data based on their sum being greater than 1,000,000.
If cell A in that row is less than 1,000,000 it will go to the next row and add up the previous cell A to the current one, and so on. This continues until the sum of all rows >= 1,000,000. When that happens, a number is "assigned" (as in entered at the end of the rows).
Sample data:
Here is my current "pseudo" code:
For x = 2 to lastrow
y = 1
If Range("A" & x).value < 1000000 Then
'I know something needs to be entered here but I don't know what
Do while balance < 1000000
sumbalance = Range("A" & x) + Range("A" & x + 1)
'Until sumbalance >= 1000000 Then Range("A" & x).Offset(0, 2).value = y
Else
Range("A" & x).offset(0, 2).value = y + 1 '(?)
Next x
Can someone point me the in the right direction?
With 50K rows, you will likely appreciate moving the values into a variant array for processing then returning them to the worksheet en masse.
Dim i As Long, rws As Long, dTTL As Double, v As Long, vVALs As Variant
With Worksheets("Sheet2")
vVALs = .Range(.Cells(2, "A"), .Cells(.Cells(Rows.Count, "A").End(xlUp).Row, "B")).Value2
For v = LBound(vVALs, 1) To UBound(vVALs, 1)
dTTL = dTTL + vVALs(v, 1): rws = rws + 1
If dTTL >= 10 ^ 6 Then
For i = v - rws + 1 To v
vVALs(i, 2) = rws
Next i
dTTL = 0: rws = 0
End If
Next v
.Cells(2, "A").Resize(UBound(vVALs, 1), UBound(vVALs, 2)) = vVALs
End With
It isn't clear how you wanted to end the sequence if the last set of numbers do not reach the 1M mark.
I hope i am clear in my comments, let me know if the code does what you want.
Option Explicit
Sub balance()
Dim wb As Workbook
Dim ws As Worksheet
Dim x As Double, y As Integer
Dim lastrow As Long
Dim sumbalance As Double
Dim Reached As Boolean
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1") 'Change the name of the sheet to yours
lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row 'Check the last Row
For x = 2 To lastrow
y = 1 ' Number 1 will be past in column C when sumblance >= 1'000'000
Reached = False
Do
If Range("A" & x).Value < 10 ^ 6 Then ' Value less than 1'000'000
If sumbalance = 0 Then 'Start the sum balance at 0
sumbalance = Range("A" & x)
Else
sumbalance = Range("A" & x) + sumbalance 'We add the previous amount to the new one
x = x + 1
End If
Else
Range("A" & x).Offset(0, 2).Value = y ' If the number is directly >= 1'000'000
Reached = True
End If
Loop Until sumbalance >= 10 ^ 6 Or x = lastrow Or Reached = True
Range("A" & x).Offset(0, 2).Value = y 'when the Sum Balance is >= 1'000'000 so 1 is paste in column c
sumbalance = 0 'Reinitialize the balance to 0
Next x
End Sub
I have a macro that runs 4 formulas.
Sub Kit()
Dim ws As Worksheet
Dim LastRow As Long
Dim i, n, x As Integer
Set ws = ActiveWorkbook.Sheets("Report KIT (2)")
ws.Select
LastRow = Sheets("Report KIT (2)").Range("A" & Sheets("Report KIT (2)").Rows.Count).End(xlUp).Row
For i = 3 To LastRow
On Error Resume Next
If Range("BR" & i) >= Range("AM" & i) Then
Range("BS" & i) = "C"
Else: Range("BS" & i) = "GA + C"
End If
Next i
For i = 3 To LastRow
On Error Resume Next
Range("BT" & i).Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[-1]=""C"",(RC[-3]/SUMIFS(C[-3],C[-6],RC[-6]))*(VLOOKUP('Report KIT (2)'!RC[-6],GA_C!C[-71]:C[-68],4,0)),SUM((RC[-3]/SUMIFS(C[-3],C[-6],RC[-6]))*(VLOOKUP('Report KIT (2)'!RC[-6],GA_C!C[-71]:C[-68],4,0)),(RC[-3]/SUMIFS(C[-3],C[-6],RC[-6],C[-1],""GA + C""))*(VLOOKUP('Report KIT (2)'!RC[-6],GA_C!C[-71]:C[-69],3,0))))"
Next i
For i = 3 To LastRow
On Error Resume Next
Range("BU" & i).Select
ActiveCell.FormulaR1C1 = "=RC[-4]+RC[-1]"
Next i
For i = 3 To LastRow
On Error Resume Next
Range("BV" & i).Select
ActiveCell.FormulaR1C1 = "=(RC[-2]+RC[-5])*0.13"
Next i
End Sub
I would like to modify it in order to repeat the same calculation, but after each full circle of all 4 formulas to move starting columns: BS; BT; BU; BV in 4 cells forward (so on the next circle they become BW; BX; BY; BZ, then on the 3rd run CA; CB; CC; CD etc.) And i would like to loop it for 11 times. Can anyone help with it, please?
You can try the below. I have referenced the columns with numbers, using the cells property. After each formula loop the column increments by 1.
Also remember that if you declare variables like this Dim i, n, x As Integer it will only declare x as an integer, i and n will be declared as variants.
Option Explicit
Sub Kit()
Dim ws As Worksheet
Dim LastRow As Long
Dim i As Integer, n As Integer, x As Integer, j As Integer, mcol As Integer
Set ws = ActiveWorkbook.Sheets("Report KIT (2)")
ws.Select
LastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
mcol = 71
For j = 1 To 11
For i = 3 To LastRow
On Error Resume Next
If Cells(i, mcol - 1) >= Range("AM" & i) Then
Cells(i, mcol) = "C"
Else
Cells(i, mcol) = "GA + C"
End If
Next i
mcol = mcol + 1
For i = 3 To LastRow
On Error Resume Next
Cells(i, mcol) = "D" ''formula using mcol
Next i
mcol = mcol + 1
For i = 3 To LastRow
On Error Resume Next
Cells(i, mcol) = "E" ''formula using mcol
Next i
mcol = mcol + 1
For i = 3 To LastRow
On Error Resume Next
Cells(i, mcol) = "F" ''formula using mcol
Next i
mcol = mcol + 1
Next j
End Sub
You need 2 new loops and change the range method to cells method
For mainLoop = 1 To 11
For newLoop = 0 To 4
'demonstration of the change
'in EDIT added the (newLoop * 4) * mainLoop into the column increment
For i = 3 To LastRow
If Cells(i, 70 + (newLoop * 4) * mainLoop ) >= Cells(i, 39) Then 'change the right part of compare >= as needed
Cells(i, 71 + (newLoop * 4) * mainLoop ) = "C"
Else: Cells(i, 71 + (newLoop * 4)*mainLoop ) = "GA + C"
End If
Next i
'repeat similar change in all other loops
For i = 3 To LastRow
'...
Next i
For i = 3 To LastRow
'...
Next i
For i = 3 To LastRow
'...
Next i
Next newLoop
Next mainLoop
Edit 2
After correct comments from the author of the question...This should do the trick.
For mainLoop = 0 To 10
For newLoop = 0 To 3 'changed to 3
For i = 3 To LastRow
If Cells(i, 70 + newLoop * 4 + 16 * mainLoop) >= Cells(i, 39) Then 'change the right part of compare >= as needed
Cells(i, 71 + newLoop * 4 + 16 * mainLoop) = "C"
Else: Cells(i, 71 + newLoop * 4 + 16 * mainLoop) = "GA + C"
End If
Next i
'repeat similar change in all other loops
For i = 3 To LastRow
'...
Next i
'...
Next newLoop
Next mainLoop
Please help me with some advice regarding the below excel. In the incipient form looks like this:
A B C
1 A1 ;100;200;300;400;500;
2 A2 ;716;721;428;1162;2183;433;434;1242;717;718;
3 A3 ;100;101;
And i want to reach this result:
A B C
1 A1 100
1 200
1 300
1 400
1 500
2 A2 716
2 721
2 428
2 1162
2 2183
2 433
2 434
2 1242
2 717
2 718
3 A3 100
3 101
I tried using this code, but it does not return the expected result.
Sub SliceNDice()
Dim objRegex As Object
Dim X
Dim Y
Dim lngRow As Long
Dim lngCnt As Long
Dim tempArr() As String
Dim strArr
Set objRegex = CreateObject("vbscript.regexp")
objRegex.Pattern = "^\s+(.+?)$"
'Define the range to be analysed
X = Range([a1], Cells(Rows.Count, "b").End(xlUp)).Value2
ReDim Y(1 To 2, 1 To 1000)
For lngRow = 1 To UBound(X, 1)
'Split each string by ";"
tempArr = Split(X(lngRow, 2), ";")
For Each strArr In tempArr
lngCnt = lngCnt + 1
'Add another 1000 records to resorted array every 1000 records
If lngCnt Mod 1000 = 0 Then ReDim Preserve Y(1 To 2, 1 To lngCnt + 1000)
Y(1, lngCnt) = X(lngRow, 1)
Y(2, lngCnt) = objRegex.Replace(strArr, "$1")
Next
Next lngRow
'Dump the re-ordered range to columns C:D
[c1].Resize(lngCnt, 2).Value2 = Application.Transpose(Y)
End Sub
Thanks in advance!
Try this:
Option Explicit
Sub DoSomething()
Dim i As Integer, j As Integer, k As Integer
Dim srcwsh As Worksheet, dstwsh As Worksheet
Dim sTmp As String, sNumbers() As String
Set srcwsh = ThisWorkbook.Worksheets("Sheet1")
Set dstwsh = ThisWorkbook.Worksheets("Sheet2")
i = 1
j = 1
Do While srcwsh.Range("A" & i) <> ""
sTmp = srcwsh.Range("C" & i)
sNumbers = GetNumbers(sTmp)
For k = LBound(sNumbers()) To UBound(sNumbers())
dstwsh.Range("A" & j) = srcwsh.Range("A" & i)
dstwsh.Range("B" & j) = srcwsh.Range("B" & i)
dstwsh.Range("C" & j) = sNumbers(k)
j = j + 1
Next
i = i + 1
Loop
Set srcwsh = Nothing
Set dstwsh = Nothing
End Sub
Function GetNumbers(ByVal sNumbers As String) As String()
Dim sTmp As String
sTmp = sNumbers
'remove first ;
sTmp = Left(sTmp, Len(sTmp) - 1)
'remove last ;)
sTmp = Right(sTmp, Len(sTmp) - 1)
GetNumbers = Split(sTmp, ";")
End Function
Note: i'd suggest to add error handler. For further information, please see: Exception and Error Handling in Visual Basic
This code will work for you
Sub SplitAndCopy()
Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("YourTargetSheet")
Dim i As Long, j As Long, k As Long
k = 2
For i = 2 To Cells(Rows.Count, "A").End(xlUp).Row
For j = LBound(Split(Range("C" & i).Value, ";")) + 1 To UBound(Split(Range("C" & i).Value, ";")) - 1
sh.Range("A" & k).Value = Range("A" & i).Value
If j = LBound(Split(Range("C" & i).Value, ";")) + 1 Then
sh.Range("B" & k).Value = Range("B" & i).Value
End If
sh.Range("C" & k).Value = Split(Range("C" & i).Value, ";")(j)
k = k + 1
Next j
Next i
End Sub
I would rather go this way:
Private Type data
col1 As Integer
col2 As String
col3 As String
End Type
Sub SplitAndCopy()
Dim x%, y%, c%
Dim arrData() As data
Dim splitCol() As String
ReDim arrData(1 To Cells(1, 1).End(xlDown))
x = 1: y = 1: c = 1
Do Until Cells(x, 1) = ""
arrData(x).col1 = Cells(x, 1)
arrData(x).col2 = Cells(x, 2)
arrData(x).col3 = Cells(x, 3)
x = x + 1
Loop
[a:d].Clear
For x = 1 To UBound(arrData)
Cells(c, 2) = arrData(x).col2
splitCol = Split(Mid(arrData(x).col3, 2, Len(arrData(x).col3) - 2), ";")
' sort splitCol
For y = 0 To UBound(splitCol)
Cells(c, 1) = arrData(x).col1
Cells(c, 3) = splitCol(y)
c = c + 1
Next y
Next x
End Sub
I am not totally sure if you need your third column sorted, in case you can add a sorting function.
I've made a code that copy values between workbooks.
The problem is it is too slow (it takes almost 30 minutes to copy to 60 files).
I think it's because I set value for each cell.
For Each cl In rg
For c = 0 To 4
wb.ActiveSheet.Cells(i + c, 2 + n).Value = cl.Offset(r - 2, c).Value
Next
n = n + 1
Next
The reason I do it is the task: there are 60 rows of cells (there is a formula in each cell) (550 cells in each row). Values (results, not formulas) of first row must be copied to the first excel workbook (there are 60 files), second row to the second workbook, etc. This row is copied in the table 5x110 where data is filled by columns (first 5 cells of the row - is the first column, etc.).
How to optimize this? (I've tried copy - past values - becomes not responding).
I've already done opening Excel Application in invisible mode.
I haven't tried to write to the closed excel file (without opening it) yet (but I think it will not become working much faster)
Sub CopyM()
Dim rg As Range, r As Long, c As Long, wb As Excel.Workbook, col As Long, i As Long, j(1 To 60) As String, k As Long
Dim FileName As String
Dim app As New Excel.Application
Dim FolderPath As String, p As String, cl As Range, n As Long
app.Visible = False
i = 2
For k = 1 To 60
If k < 51 Then
j(k) = k
Else
j(k) = ("d" & (k - 50))
End If
Next k
Set rg = Range("K2")
Application.ScreenUpdating = False
For col = 16 To 560 Step 5
Set rg = Union(rg, Cells(2, col))
Next col
p = ActiveWorkbook.Path
FolderPath = (p & "\")
FileName = (FolderPath & j(1) & ".xlsm")
n = 0
For r = 2 To 61
FileName = (FolderPath & j(r - 1) & ".xlsm")
Set wb = app.Workbooks.Open(FileName)
n = 0
For Each cl In rg
For c = 0 To 4
wb.ActiveSheet.Cells(i + c, 2 + n).Value = cl.Offset(r - 2, c).Value
Next
n = n + 1
Next
wb.Close savechanges:=True
app.Quit
Application.ScreenUpdating = True
Cells(1, 1).Value = (r - 1) & "/60"
Application.ScreenUpdating = False
Next
Set app = Nothing
Application.ScreenUpdating = True
Cells(1, 1).Value = ""
MsgBox "Finished"
End Sub
That's awesome!!
The time of execution significantly reduced to 3 minutes 19 seconds!
Thank you #chrisneilsen for suggestion!
Here is the edited code:
Sub CopyM()
Dim r As Long, wb As Excel.Workbook, i As Long, p As String, n As Long
Dim FileName As String, j(1 To 60) As String, k As Long
Dim app As New Excel.Application
Dim FolderPath As String, ai As Variant, bi(1 To 5, 1 To 110) As Variant
app.Visible = False
For k = 1 To 60
If k < 51 Then
j(k) = k
Else
j(k) = ("d" & (k - 50))
End If
Next k
Application.ScreenUpdating = False
p = ActiveWorkbook.Path
FolderPath = (p & "\")
FileName = (FolderPath & j(1) & ".xlsm")
r = 2
i = 0
n = 1
For r = 2 To 61
ai = Range(Cells(r, 11), Cells(r, 560)).Value
i = 0
n = 1
For i = 1 To 550 Step 5
bi(1, n) = ai(1, i)
bi(2, n) = ai(1, 1 + i)
bi(3, n) = ai(1, 2 + i)
bi(4, n) = ai(1, 3 + i)
bi(5, n) = ai(1, 4 + i)
n = n + 1
Next
FileName = (FolderPath & j(r - 1) & ".xlsm")
Set wb = app.Workbooks.Open(FileName)
wb.ActiveSheet.Range("B2:DG6").Value = bi
wb.Close savechanges:=True
app.Quit
Application.ScreenUpdating = True
Cells(1, 1).Value = (r - 1) & "/60"
Application.ScreenUpdating = False
Next
Set app = Nothing
Application.ScreenUpdating = True
Cells(1, 1).Value = ""
MsgBox "Finished"
End Sub
Not sure how to put this in words, but basically macro is run from the sheet1 of WorkBook1, and it should produce one like sheet1 of WorkBook2. (WB2 Sheet1 is empty)
The trick is that macro should only work with user selected range.
So if A1:A7 is selected, it will only grab data from A1:A7 to last column with data
If nothing is selected then exit sub with msgbox or something
Order/sort does not matter as long as it merges XYs duplicates and group respective fruits together.
A B => A B C
1 XY3 Apple => 1 H XY1
2 XY1 Orange => 2 D Orange
3 XY3 Banana => 3 H XY2
4 XY3 Banana => 4 D Orange
5 XY3 Peach => 5 H XY3
6 XY4 Orange => 6 D Apple
7 XY2 Orange => 7 D Banana
8 XY7 Apple => 8 D Banana
=> 9 D Peach
=> 10 H XY4
=> 11 D Orange
[WB1 Sheet1] => [WB2 Sheet1]
This might be difficult but I am desperately seeking for help.
Thank you so much!
I set up this macro to copy to sheet2 of the same workbook. To save to a new workbook just update the following line of code with your workbook name instead of activeworkbook.
Set sheetB = ActiveWorkbook.Sheets("Sheet2")
Started with the following data in sheet 1 and a blank sheet 2:
Select A1 to A8 and run this macro:
Sub CopyAndFormat()
If IsEmpty(Selection) Then
MsgBox ("Empty Cell")
Exit Sub
End If
Dim sheet As Worksheet
Set sheetA = ActiveWorkbook.Sheets("Sheet1")
Set sheetB = ActiveWorkbook.Sheets("Sheet2")
Dim FirstRow As Long, LastRow As Long
FirstRow = Selection.Rows(1).Row
LastRow = Selection.Rows.Count + FirstRow - 1
'First Column
Dim rngA As Range
Set rngA = Range("A" & FirstRow & ":A" & LastRow)
Dim datA As Variant
datA = rngA
Dim i As Long
'Second Column Match
Dim rngB As Range
Set rngB = Range("B" & FirstRow & ":B" & LastRow)
Dim datB As Variant
datB = rngB
Dim j As Long
Dim resultA As Variant
Dim resultB As Variant
Dim rng As Range
Dim rngr As Range
Set rng = sheetB.Range("A1:A" & LastRow + 100)
Set rngr = sheetB.Range("B1:B" & LastRow + 100)
resultA = rng
resultB = rngr
'Store duplicates
Dim rngString As String
rngString = "empty"
Dim match As Boolean
match = False
Dim cntr As Integer
cntr = 1
'First Column loop
For i = LBound(datA, 1) To UBound(datA, 1)
If rngString <> "empty" Then
If Not Intersect(Range("A" & i), Range(rngString)) Is Nothing Then
GoTo nextloop
End If
End If
'Second Column Loop
For j = LBound(datA, 1) + i To UBound(datA, 1)
If i <> j And datA(i, 1) = datA(j, 1) And Not IsEmpty(datA(j, 1)) And Not IsEmpty(datA(i, 1)) Then
'copy position of duplicate in variant
If rngString = "empty" Then
match = True
resultA(cntr, 1) = datA(i, 1)
resultB(cntr + 1, 1) = datB(i, 1)
resultB(cntr + 1, 1) = datB(i, 1)
resultB(cntr + 2, 1) = datB(j, 1)
rngString = "A" & i & ",A" & j
cntr = cntr + 2
Else
resultB(cntr + 1, 1) = datB(j, 1)
cntr = cntr + 1
rngString = rngString & "," & "A" & j
End If
End If
Next
If match = False Then
resultA(cntr + 1, 1) = datA(i, 1)
resultB(cntr + 2, 1) = datB(i, 1)
cntr = cntr + 2
End If
match = False
'cntr = cntr + 1
nextloop:
Next
rng = resultA
rngr = resultB
End Sub
You'll get the following on sheet2:
Sorry the code is a little messy and I hate using goto's but this will get you started.