Last active
August 18, 2022 14:22
-
-
Save Vincent-233/16a5eb511d182974102e9747d4e12ab4 to your computer and use it in GitHub Desktop.
VBA_Working
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub copyJira() | |
Dim table_name As String | |
Dim row_cnt As Integer, col_cnt As Integer | |
Dim i As Integer, j As Integer | |
Set wks_main = Workbooks("macro_study_02.xlsx").Worksheets("Main_JIRA") | |
Set wks_link = Workbooks("macro_study_02.xlsx").Worksheets("Link_JIRA") | |
Set table_obj = wks_main.ListObjects(1) 'get first Table in sheet | |
'copy jira | |
table_name = table_obj.Name | |
col_cnt = table_obj.ListColumns.Count | |
row_cnt = table_obj.ListRows.Count | |
'copy header to link_jira sheet | |
wks_link.Activate | |
wks_link.Cells.Clear | |
table_obj.HeaderRowRange.Copy wks_link.Cells(1, 1) | |
'copy each row three time to link_jira sheet | |
For i = 1 To row_cnt ' for each row | |
For j = 1 To 3 ' copy 2 times | |
table_obj.ListRows(i).Range.Copy wks_link.Cells((i - 1) * 3 + j + 1, 1) | |
Next j | |
Next i | |
'update after copy | |
' add 3 link cols | |
wks_link.Range(Cells(1, col_cnt + 1), Cells(1, col_cnt + 3)).Value = Array("Link BA", "Link Dev", "Link QA") | |
' update jira name value ( BA/DEV/QA ) and link no | |
Dim type_col As Integer, key_col As Integer, summary_col As Integer | |
type_col = wks_link.Range(Cells(1, 1), Cells(1, col_cnt)).Find("Type").Column | |
key_col = wks_link.Range(Cells(1, 1), Cells(1, col_cnt)).Find("Key").Column | |
summary_col = wks_link.Range(Cells(1, 1), Cells(1, col_cnt)).Find("Summary").Column | |
' jira type change to Task | |
wks_link.Range(Cells(2, type_col), Cells(row_cnt * 3 + 1, type_col)).Value = "Task" | |
' jira summary and link jira | |
Dim prefixStr As String, offsetNum As Integer | |
For i = 2 To row_cnt * 3 + 1 | |
If (i - 1) Mod 3 = 1 Then | |
prefixStr = "[BA] " | |
offsetNum = 1 | |
End If | |
If (i - 1) Mod 3 = 2 Then | |
prefixStr = "[Dev] " | |
offsetNum = 2 | |
End If | |
If (i - 1) Mod 3 = 0 Then | |
prefixStr = "[QA] " | |
offsetNum = 3 | |
End If | |
wks_link.Cells(i, summary_col).Value = prefixStr & Cells(i, summary_col).Value | |
wks_link.Cells(i, col_cnt + offsetNum).Value = wks_link.Cells(i, key_col).Value | |
Next i | |
'clear / delete columns | |
' clear key / jira no | |
wks_link.Range(Cells(2, key_col), Cells(row_cnt * 3 + 1, key_col)).Clear | |
' clear sprint / epic link ( optional ) | |
' to do | |
' delete all columns not in specified list ( maintain list in a sheet ) | |
' to do | |
End Sub | |
Sub SaveAsCSV() | |
End Sub | |
Sub utf8_remove_BOM(FileIn As String, FileOut As String) | |
' ADODB.Stream file I/O constants | |
Const adTypeBinary = 1 | |
Const adTypeText = 2 | |
Const adSaveCreateNotExist = 1 | |
Const adSaveCreateOverWrite = 2 | |
Const adModeReadWrite = 3 | |
Dim srcStream As Object | |
Dim destStream As Object | |
Set srcStream = CreateObject("ADODB.Stream") | |
srcStream.Type = adTypeText | |
srcStream.Mode = adModeReadWrite | |
srcStream.Charset = "utf-8" | |
srcStream.Open | |
srcStream.LoadFromFile FileIn | |
Set destStream = CreateObject("ADODB.Stream") | |
destStream.Type = adTypeBinary | |
destStream.Mode = adModeReadWrite | |
destStream.Open | |
srcStream.Position = 3 ' skip BOM, see more in Byte_order_mark in wikipedia | |
srcStream.CopyTo destStream | |
srcStream.Flush | |
srcStream.Close | |
Set srcStream = Nothing | |
destStream.SaveToFile FileOut, adSaveCreateOverWrite | |
destStream.Flush | |
destStream.Close | |
Set destStream = Nothing | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment